package com.dayu.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class Renderer {
}


class CheckBoxListRenderer extends JCheckBox implements ListCellRenderer<JCheckBox> {
    private int hoverIndex = -1;
    private final Color hoverColor = Color.decode("#a3b8cc");

    public CheckBoxListRenderer(JList<JCheckBox> list) {
        list.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                int index = list.locationToIndex(e.getPoint());
                if (index != hoverIndex) {
                    hoverIndex = index;
                    list.repaint();
                }
            }
        });
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends JCheckBox> list, JCheckBox value, int index, boolean isSelected, boolean cellHasFocus) {
        setEnabled(list.isEnabled());
        setSelected(value.isSelected());
        setFont(list.getFont());
        setText(value.getText());
        if (index == hoverIndex) {
            setBackground(hoverColor);
        } else {
            setBackground(list.getBackground());
        }
        setForeground(list.getForeground());
        return this;
    }
}


class ToolTipRenderer extends DefaultListCellRenderer {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (component instanceof JComponent) {
            JComponent jComponent = (JComponent) component;
            String text = value.toString();
            jComponent.setToolTipText(text);
        }

        return component;
    }
}
